﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using RaidTeamManager.ViewModel;

namespace RaidTeamManager
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            Closing += (s, e) => ViewModelLocator.Cleanup();
        }

        private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            var pd = (System.ComponentModel.PropertyDescriptor) e.PropertyDescriptor;
            e.Column.Header = pd.DisplayName;
        }

        private void MainWindow_OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is MainViewModel)
            {
                var worker = new BackgroundWorker();
                worker.DoWork += (o, args) =>
                    {
                        var model = (MainViewModel) e.NewValue;
                        model.UpdateClasses();
                    };
                worker.RunWorkerAsync();
            }
        }
    }
}